<?php
namespace app\admin\controller;



use app\common\model\Ueditor as UeditorModel;
use app\common\controller\AdminBase;
use think\Db;
use think\facade\Log;

class Other extends AdminBase
{
    protected $ueditor_model;

    protected function initialize()
    {
        parent::initialize();
        $this->ueditor_model = new UeditorModel();
    }

    /**
     * 富文本编辑器
     */
    public function ueditor()
    {
        //富文本编辑器
        $data = $this->ueditor_model->where('status',1)->find();
        return $this->fetch('ueditor', ['data'=>$data]);
    }

    //树行菜单
    public function tree()
    {
        //富文本编辑器
        $city = $this->getcity();
        return $this->fetch('tree/tree', ['city'=>json_encode($city)]);
    }

    public function getcity(){
        $swhere = "1=1";
        $plist = Db::name('citydata')->field('Id,name AS topic')->where('pid=0')->order('ord ASC,Id ASC')->select();

        if ( $plist ) {
            foreach( $plist as $pk=>$pv ) {
                $cdata =  Db::name('citydata')->field('Id,name AS topic')->where('pid='.$pv['Id'])->order('ord ASC,Id ASC')->select();
                if ( $cdata ) {
                    foreach( $cdata as $ck=>$cv ) {
                        $school = Db::name('school')->field('Id,schoolname AS topic,schoolcode')->where($swhere." AND cityid=".$cv['Id'])->select();
                        if ( $school ) {
                            foreach( $school as $sk=>$sv ) {
                                $school[$sk]['topic'] = $sv['schoolcode'].$sv['topic'];
                            }
                        }
                        $cdata[$ck]['childs'] = $school;
                    }
                }
                $plist[$pk]['childs'] = $cdata;
            }
        }
        return $plist;
    }



    /**
     * 编辑模版
     * @param $id
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $data			= $this->request->post();

            if ($this->ueditor_model->update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }


    //日志写入测试
    public function logtest(){
        $info = [
            'url'=>'http://baidu.com',
            'info'=>'测试日志信息，这是error日志',
        ];
        Log::write($info,'error');
        Log::write('测试日志信息，这是sql日志','sql');
        Log::write('测试日志信息，这是api日志','api');
    }


}